<?php
/**
 * Created by b17
 * 3/5/12 6:42 PM
 */

namespace Managers\Builder;

class CommentBuilder
{
    /**
     * @var \Doctrine\ORM\QueryBuilder
     */
    protected $qb;

    public function __construct()
    {
        $this->reset();
    }

    public function reset()
    {
        $manager = new \Managers\Comments();
        $this->qb = $manager->createQueryBuilder();
    }

    public function setLimit($limit, $offset = 0)
    {
        $this->qb->setMaxResults($limit)
            ->setFirstResult($offset);
        return $this;
    }

    public function setUser($userId)
    {
        $this->qb->andWhere('u.id = :user')->setParameter('user', $userId);
        return $this;
    }

    public function setId($id)
    {
        $this->qb->andWhere('c.id = :id')->setParameter('id', $id);
        return $this;
    }

    public function getTotal()
    {
        $qb = clone $this->qb;
        $qb->select('count(c.id) as total');
        $res = $qb->getQuery()->getArrayResult();
        return $res[0]['total'];
    }

    public function getList()
    {
        $res = $this->qb->getQuery()->getArrayResult();
        return $res;
    }

    public function getResult()
    {
        $res = $this->qb->getQuery()->getResult();
        return $res;
    }

    public function getOne()
    {
        $res = $this->qb->getQuery()->getArrayResult();
        return count($res) ? $res[0] : false;
    }

    public function addOrderById($order = 'DESC')
    {
        $this->qb->addOrderBy('c.id', $order);
        return $this;
    }

    public function setIsBlocked($isBlocked)
    {
        $this->qb->andWhere('c.isBlocked = :blocked')->setParameter('blocked', $isBlocked);
        return $this;
    }
}
